TableMenuItemDispatcher   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
c 0
b 0
f 0
dl 0
loc 30
rs 10
wmc 6

2 Functions

Rating   Name   Duplication   Size   Complexity  
A getMenuItem 0 24 5
A isAvailable 0 3 1
1
import AbstractMenuItemDispatcher from './AbstractMenuItemDispatcher';
2
import { svgIcon } from '../MDI';
3
import MenuItem from '../MenuItem';
4
import { setBlockTypeNoAttrCheck } from '../../../customCommands';
5
6
export default class TableMenuItemDispatcher extends AbstractMenuItemDispatcher {
7
    static isAvailable(schema) {
8
        return !!schema.nodes.table;
9
    }
10
11
    static getMenuItem(schema) {
12
        if (!this.isAvailable(schema)) {
13
            throw new Error('Table not available in schema!');
14
        }
15
        return new MenuItem({
16
            command: (state, dispatch) => {
17
                if (!setBlockTypeNoAttrCheck(schema.nodes.table)(state)) {
18
                    return false;
19
                }
20
                if (dispatch) {
21
                    const tableCell = schema.nodes.table_cell.create({}, schema.nodes.paragraph.create());
22
                    const rowCells = [tableCell, tableCell.copy(tableCell.content)];
23
                    const tableRow = schema.nodes.table_row.create({}, rowCells);
24
                    const tableRows = [tableRow, tableRow.copy(tableRow.content)];
25
                    const tableNode = schema.nodes.table.create({}, tableRows);
26
                    dispatch(state.tr.replaceSelectionWith(tableNode));
27
                }
28
29
                return true;
30
            },
31
            icon: svgIcon('table-plus'),
32
            label: LANG.plugins.prosemirror['label:table'],
0 ignored issues
show
Bug introduced by
The variable LANG seems to be never declared. If this is a global, consider adding a /** global: LANG */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
33
        });
34
    }
35
}
36